//
//  LGPatientCardCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/23.
//  患者卡片

#import "LGPatientCardCell.h"
#import "TIMDefine.h"
@interface LGPatientCardCell()
/**   */
@property (nonatomic , strong)UILabel *patientName;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSMutableArray *btnArray;
/**   */
@property (nonatomic , assign)CGFloat totalH;
@end

@implementation LGPatientCardCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}


-(void)fillWithData:(LGPatientCardData *)data{
    [super fillWithData:data];
    NSDictionary *dataDic = data.customDic[@"desc"];
    NSString *age = ([dataDic[@"age"] integerValue]>0)?[NSString stringWithFormat:@"%@岁",dataDic[@"age"]]:@"";
    self.patientName.text = [NSString stringWithFormat:@"%@  %@  %@",dataDic[@"name"],dataDic[@"sex"],age];
    self.contentLabel.text = LGNSString(dataDic[@"content"]);
    CGFloat contentH = [LGNSString(dataDic[@"content"]) textHeightWithFont:LGFont(16) maxWidth:viewPix(207)];
    NSArray *itemArray = dataDic[@"ques"];
    for (UIButton *button in self.btnArray) {
        [button removeFromSuperview];
    }
    [self.btnArray removeAllObjects];
    CGFloat maxWidth = viewPix(207);
    CGFloat left = 0;
    CGFloat top = 0;
    for (NSString *content in itemArray) {
        CGFloat contentW = [content textWidthWithFont:LGFont(16) maxHeight:maxWidth]+viewPix(24);
        top = (left+contentW>maxWidth)?top+viewPix(32):top;
        left = (left+contentW>maxWidth)?0:left;
        UIButton *button = [UIButton buttonWithTitle:content titleFont:LGFontWeight(12, UIFontWeightSemibold) textColor:@"#796DF5" frame:CGRectMake(left, top, contentW, viewPix(24))];
        [button addBorder:@"#796DF5" lineWidth:1 cornerRidus:viewPix(12)];
        [self.itemView addSubview:button];
        [self.btnArray addObject:button];
        left += contentW+viewPix(8);
    }
}

+(CGFloat)getEstimatedHeight:(LGPatientCardData *)data{
    NSDictionary *dataDic = data.customDic[@"desc"];
    CGFloat contentH = [LGNSString(dataDic[@"content"]) textHeightWithFont:LGFont(16) maxWidth:viewPix(207)];
    NSArray *itemArray = dataDic[@"ques"];
    CGFloat maxWidth = viewPix(207);
    CGFloat left = 0;
    CGFloat top = contentH+viewPix(45)+viewPix(26);
    for (NSString *content in itemArray) {
        CGFloat contentW = [content textWidthWithFont:LGFont(16) maxHeight:maxWidth]+viewPix(24);
        top = (left+contentW>maxWidth)?top+viewPix(32):top;
        left = (left+contentW>maxWidth)?0:left;
        left += contentW+viewPix(8);
    }
    CGFloat height = top+viewPix(35);
    
    return height;
}

+(CGSize)getContentSize:(LGPatientCardData *)data{
    CGFloat height = [LGPatientCardCell getEstimatedHeight:data];
    return CGSizeMake(viewPix(245), height);
}

+(CGFloat)getHeight:(TUIMessageCellData *)data withWidth:(CGFloat)width{
    CGFloat height = [LGPatientCardCell getEstimatedHeight:data];
    return height+35;
}


-(void)createSubView{
    self.container.cornerRidus = 15;
    self.container.backgroundColor = [UIColor colorWithString:@"#DEDCF1"];
    [self.container addSubview:self.patientName];
    [self.container addSubview:self.contentLabel];
    [self.container addSubview:self.itemView];
    
    [self.patientName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.container).offset(viewPix(13));
        make.left.equalTo(self.container).offset(viewPix(12));
        make.right.equalTo(self.container).offset(-viewPix(12));
        make.height.equalTo(@(viewPix(22)));
    }];

    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.patientName.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.patientName);
        make.width.equalTo(@(viewPix(207)));
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(self.patientName);
        make.bottom.equalTo(self.container).offset(-viewPix(15));
    }];
}

-(UILabel *)patientName{
    if(!_patientName){
        _patientName = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _patientName;
}


-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}

-(NSMutableArray *)btnArray{
    if(!_btnArray){
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


#pragma mark
#pragma mark ====> LGPatientCardData

@implementation LGPatientCardData
+ (LGPatientCardData *)getCellData:(V2TIMMessage *)message{
    LGPatientCardData *cellData = [[LGPatientCardData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];
    cellData.showName = NO;
    cellData.showAvatar = YES;
    cellData.innerMessage = message;
    cellData.msgID = message.msgID;
    cellData.avatarUrl = [NSURL URLWithString:message.faceURL];
    cellData.customDic = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    return cellData;
}

/**
 * Getting the display string according to the message
 */
+(NSString *)getDisplayString:(V2TIMMessage *)message{
    NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    if (!param || ![param isKindOfClass:[NSDictionary class]]) {
        return @"[自定义消息]";
    }
    NSString *businessID = param[BussinessID];
    if([businessID isEqualToString:@"TIMCustom_01"]){
        return @"[患者档案]";
    }
    
    return @"[自定义消息]";
}

@end
